package com.demo.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

/**
 * @Author: lixiaodong
 * @Description:  图形验证码
 * @Date: 2022/9/15 15:12
 * @Parms:
 * @ReturnType:
 */

@Slf4j
@RestController
@Api(tags = "图形验证码")
public class LoginController extends BaseController {
    @Autowired
    DefaultKaptcha defaultKaptcha;
    @GetMapping("ecode")
    public void code(HttpSession session , HttpServletResponse res) throws Exception{
        String code = defaultKaptcha.createText();
        session.setAttribute("code", code);
        log.info("code:{}",code);
        BufferedImage image = defaultKaptcha.createImage(code);
        ImageIO.write(image, "jpg", res.getOutputStream());
    }


}
